package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.admin.utils.buckettool.BaseBucketTool;
import com.iot08.common.entity.admin.MobNotify;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.response.Response;
import com.iot08.common.service.base.BaseAdminService;
import com.iot08.common.utils.StringUtil;
import com.iot08.common.utils.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Description: 广告业务逻辑
 * @time: 2020/10/22 11:40
 * @author: Rock
 */
@Slf4j
@Service
public class MobNotifyService extends BaseAdminService<MobNotify> {

    @Resource
    private UserService userService;
    @Resource
    private OperationService operationService;
    @Resource
    private BaseBucketTool baseBucketTool;

    /**
     * @param page
     * @param type
     * @param user
     * @time: 2020/12/26 10:49
     * @author: Rock
     * @version 1.0
     */
    public Page<MobNotify> list(PageVO page, Integer type, SysUser user) {
        PageableImpl pageable = new PageableImpl();
        page.setSort(Sort.by(Sort.Order.desc("create_time")));
        pageable.setPage(page);

        Criteria criteria = Criteria.where("deleted").is(0);
        if (StringUtils.isNotEmpty(page.getSearchValue())) {
            criteria.and("title").regex(".*?" + page.getSearchValue() + ".*?", "i");
        }
        if (type != null) {
            criteria.and("type").is(type);
        }
        Query query = new Query().addCriteria(criteria);
        long count = mongoTemplate.count(query, MobNotify.class);
        page.setTotalResult((int) count);
        List<MobNotify> list = mongoTemplate.find(query.with(pageable), MobNotify.class);
        for (MobNotify mobNotify : list) {
            mobNotify.setCreatorUser(userService.findById(mobNotify.getCreator()));
        }
        Page<MobNotify> result = new PageImpl<MobNotify>(list, pageable, count);
        return result;
    }

    /**
     * @param mobNotify
     * @param includeQRcodeImageFile
     * @param bottomBtnImageFile
     * @param user
     * @param ip
     * @time: 2020/12/26 11:25
     * @author: Rock
     * @version 1.0
     */
    public void add(MobNotify mobNotify, MultipartFile includeQRcodeImageFile,
                    MultipartFile bottomBtnImageFile, SysUser user, String ip) throws IOException {
        if (mobNotify != null) {
            if (includeQRcodeImageFile != null && !includeQRcodeImageFile.isEmpty()) {
                String suffix = includeQRcodeImageFile.getOriginalFilename().substring(includeQRcodeImageFile.getOriginalFilename().lastIndexOf("."));
                String includeQRcodeImage = BaseBucketTool.MOBNOTIFY_COMPLAINT_PATH + UUIDGenerator.generate() + suffix;
                baseBucketTool.upload(includeQRcodeImage, includeQRcodeImageFile, 1);
                mobNotify.setIncludeQRcodeImage(baseBucketTool.getDOMAIN() + includeQRcodeImage);
            }
            if (bottomBtnImageFile != null && !bottomBtnImageFile.isEmpty()) {
                String suffix = bottomBtnImageFile.getOriginalFilename().substring(bottomBtnImageFile.getOriginalFilename().lastIndexOf("."));
                String bottomBtnImage = BaseBucketTool.MOBNOTIFY_COMPLAINT_PATH + UUIDGenerator.generate() + suffix;
                baseBucketTool.upload(bottomBtnImage, bottomBtnImageFile, 1);
                mobNotify.setBottomBtnImage(baseBucketTool.getDOMAIN() + bottomBtnImage);
            }

            mobNotify.setId(UUIDGenerator.generate());
            mobNotify.setDeleted(0);
            mobNotify.setCreateTime(new Date());
            mongoTemplate.save(mobNotify);
            operationService.add(user, ip, "通知管理", "添加",
                    String.format("添加通知: %s ", mobNotify.getTitle()));
        }
    }

    /**
     * @param mobNotify
     * @param includeQRcodeImageFile
     * @param bottomBtnImageFile
     * @param user
     * @param ip
     * @time: 2020/12/26 11:37
     * @author: Rock
     * @version 1.0
     */
    public void update(MobNotify mobNotify, MultipartFile includeQRcodeImageFile,
                       MultipartFile bottomBtnImageFile, SysUser user, String ip) throws IOException {
        Update update = new Update();
        if (includeQRcodeImageFile != null && !includeQRcodeImageFile.isEmpty()) {
            String suffix = includeQRcodeImageFile.getOriginalFilename().substring(includeQRcodeImageFile.getOriginalFilename().lastIndexOf("."));
            String includeQRcodeImage = BaseBucketTool.MOBNOTIFY_COMPLAINT_PATH + UUIDGenerator.generate() + suffix;
            baseBucketTool.upload(includeQRcodeImage, includeQRcodeImageFile, 1);
            update.set("include_QRcode_image", baseBucketTool.getDOMAIN() + includeQRcodeImage);
        }
        if (bottomBtnImageFile != null && !bottomBtnImageFile.isEmpty()) {
            String suffix = bottomBtnImageFile.getOriginalFilename().substring(bottomBtnImageFile.getOriginalFilename().lastIndexOf("."));
            String bottomBtnImage = BaseBucketTool.MOBNOTIFY_COMPLAINT_PATH + UUIDGenerator.generate() + suffix;
            baseBucketTool.upload(bottomBtnImage, bottomBtnImageFile, 1);
            update.set("bottom_btn_image", baseBucketTool.getDOMAIN() + bottomBtnImage);
        }
        update.set("content", mobNotify.getContent());
        update.set("link", mobNotify.getLink());
        update.set("type", mobNotify.getType());
        update.set("update_time", new Date());
        if (StringUtil.isBlank(mobNotify.getTitle())) {
            update.unset("title");
        } else {
            update.set("title", mobNotify.getTitle());
        }
        if (StringUtil.isNotBlank(mobNotify.getSmallProgramId())) {
            update.set("small_program_id", mobNotify.getSmallProgramId());
            update.set("small_program_path", mobNotify.getSmallProgramPath());
        }
        if (StringUtil.isNotBlank(mobNotify.getJumpTipsWords())) {
            update.set("jump_tips_words", mobNotify.getJumpTipsWords());
        }
        if (StringUtil.isNotBlank(mobNotify.getAdvertFirm())) {
            update.set("advert_firm", mobNotify.getAdvertFirm());
        }
        this.updateById(mobNotify.getId(), update);
        operationService.add(user, ip, "通知管理", "编辑",
                String.format("编辑通知: %s ", mobNotify.getTitle()));
    }

    /**
     * @param id
     * @param user
     * @param ip
     * @time: 2020/10/23 9:32
     * @author: Rock
     * @version 1.0
     */
    public Response<Object> updateDel(String id, SysUser user, String ip) {
        MobNotify mobNotify = this.findById(id);
        if (mobNotify != null) {
            Update update = new Update();
            update.set("deleted", 1);
            update.set("update_time", new Date());
            this.updateById(id, update);
            operationService.add(user, ip, "通知管理", "删除",
                    String.format("删除通知: %s ", mobNotify.getTitle()));
            return Response.successResponse();
        }
        return Response.failedResponse();
    }

    /**
     * @param title
     * @time: 2020/12/26 17:06
     * @author: Rock
     * @version 1.0
     */
    public List<MobNotify> listAll(String title) {
        Criteria criteria = Criteria.where("deleted").is(0);
        if (StringUtils.isNotEmpty(title)) {
            criteria.and("title").regex(".*?" + title + ".*?", "i");
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Order.desc("create_time")));
        return this.find(query);
    }

}
